/**
 *	@file	telseq_aoi.c
 *	@brief	アオイ　会話データ抽選＆再生
 *	@author	Miyuki Iwasawa
 *	@date	09.02.06
 */

#include "common.h"
#include "system/procsys.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/fontproc.h"
#include "system/snd_tool.h"
#include "system/buflen.h"
#include "system/pm_rtc.h"
#include "gflib/touchpanel.h"
#include "field/eventflag.h"
#include "field/evwkdef.h"
#include "field/syswork.h"
#include "fielddata/script/saveflag.h"
#include "itemtool/itemsym.h"
#include "msgdata/msg.naix"
#include "msgdata/msg_pg_tel.h"
#include "msgdata/msg_tel_p_aoi.h"

#include "../pm_tel.h"
#include "tel/pmtel_def.h"
#include "tel/pmtel_datman.h"
#include "tel/pmtel_dat_def.h"
#include "../pmtel_seq.h"
#include "telseq_play.h"

static u8 sub_GetAoiTimezone(u8 hour);

typedef enum{
 AOI_TIME_MORNING,
 AOI_TIME_NOON,
 AOI_TIME_EVNING,
 AOI_TIME_WORKING,
};

/**
 *	@brief	電話　アオイの会話抽選
 */
u16 PMTelPlayL_Aoi(PMTEL_DATA_MAN* man,PMTEL_TALK_WORK* wk)
{
	wk->seq_code = TEL_SEQ_SIMPLE;

	//殿堂入りしているか？
	wk->tmp_flag1 = SysFlag_GameClearCheck( man->ev_sv );

	//アオイのタイムテーブル
	wk->tmp_u8 = sub_GetAoiTimezone(wk->rtc_time.hour);

	if(SysWork_CheckDuaringTheRadioTowerEvent(man->ev_sv)){	//ラジオ塔占拠中
		return TEL_TK_AOI_RADIO_TOWER_EVENT;
	}

	if(wk->is_receive){
		wk->seq_code = TEL_SEQ_AOI_RECEIVE;
		return 0;
	}

	//仕事中
	if(wk->tmp_u8 == AOI_TIME_WORKING){
		return TEL_TK_AOI_WORKING;
	}

	//同一マップにいるか？
	if(wk->pbk->map_id == man->zone_id)	{
		return TEL_TK_AOI_NE;
	}
	//ノーマル発信
	wk->seq_code = TEL_SEQ_AOI_CALL;
	return 0;
}

/**
 *	@brief	アオイの送信会話シーケンス
 */
int PMTelSeq_TypeAoiCall(PMTEL_DATA_MAN* man)
{
	PMTEL_TALK_WORK *wk = &man->twk;
	switch(wk->seq){
	case 0:
		//タグ展開処理
		PMTel_TagSpreadCommon(man);

		//挨拶出力
		PMTel_MsgPrintGmmSex(man,man->m_person,
			m_aoi_01_1_m+(wk->time_zone*2),m_aoi_01_1_f+(wk->time_zone*2));
		break;
	case 1:
		if(!PMTel_MsgPrintWait(man)) return FALSE;

		PMTel_MsgPrintGmm(man,man->m_person,m_aoi_03_1+gf_rand()%3);
		break;
	default:
		if(!PMTel_MsgPrintWait(man)) return FALSE;
		MSGMAN_Delete(man->m_person);
		return TRUE;
	}
	++wk->seq;
	return FALSE;
}

/**
 *	@brief	アオイの着信会話シーケンス
 */
int PMTelSeq_TypeAoiReceive(PMTEL_DATA_MAN* man)
{
	PMTEL_TALK_WORK *wk = &man->twk;
	switch(wk->seq){
	case 0:
		//タグ展開処理
		PMTel_TagSpreadCommon(man);

		//挨拶出力
		PMTel_MsgPrintGmmSex(man,man->m_person,
			m_aoi_10_1_m+(wk->time_zone*2),m_aoi_10_1_f+(wk->time_zone*2));
		break;
	case 1:
		if(!PMTel_MsgPrintWait(man)) return FALSE;
		if(wk->tmp_u8 == AOI_TIME_WORKING){
			PMTel_MsgPrintGmm(man,man->m_person,m_aoi_11_15);
		}else{
			u8 rnd = 11+(wk->tmp_flag1*3);
			rnd = gf_rand()%rnd;
			if(rnd == 13){
				PMTel_MsgPrintGmmSex(man,man->m_person,
					m_aoi_11_14_m,m_aoi_11_14_f);
			}else{
				PMTel_MsgPrintGmm(man,man->m_person,m_aoi_11_1+rnd);
			}
		}
		break;
	default:
		if(!PMTel_MsgPrintWait(man)) return FALSE;
		MSGMAN_Delete(man->m_person);
		return TRUE;
	}
	++wk->seq;
	return FALSE;
}

/*
 *	@brief	アオイのタイムゾーン取得
 */
static u8 sub_GetAoiTimezone(u8 hour)
{
	//アオイの場合、ラジオの時間帯以外だけ抽選対象に含める
	if(PMTelData_IsAoiWorking(hour)){
		return AOI_TIME_WORKING;
	}
	if(hour > 3 && hour < 10){	//朝 0
		return AOI_TIME_MORNING;
	}else if(hour > 9 && hour < 20){	//昼
		return AOI_TIME_NOON;
	}else{	//夜
		return AOI_TIME_EVNING;
	}
}